﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblActiviteTypeDAL:ImtblActiviteType
    {
        HKEA_CMSEntities model;
        mtblActiviteType activitetype;

        public mtblActiviteTypeDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblActiviteTypeDAL GetInstance()
        {
            return new mtblActiviteTypeDAL();
        }
        public int InsertENT(mtblActiviteType ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblActiviteTypes.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblActiviteType> GetListByENT(mtblActiviteType ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a=>a.ActiviteTypeUID==ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a=>a.LastModifyBy==ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.code))
                result = result.Where(a => a.code == ent.code);
            param.Count = result.Count();
            result= result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public int SaveList(List<mtblActiviteType> ToAdd, List<mtblActiviteType> ToUpdate, List<int> ToDel, string LastModifyBy)
        {
            foreach (var temp in ToAdd)
            {
                if (temp != null)
                {
                    var ent = new mtblActiviteType();
                    ent.LastModifyDate = DateTime.Now;
                    ent.LastModifyBy = LastModifyBy;
                    ent.CreationDate = DateTime.Now;
                    ent.ActiviteType_Chi = temp.ActiviteType_Chi;
                    ent.ActiviteType_Eng = temp.ActiviteType_Eng;
                    ent.code = temp.code;
                    ent.IsFromHKEA = 1;
                    ent.LastModifyBy = LastModifyBy;
                    model.mtblActiviteTypes.AddObject(ent);
                }
            }
            foreach (var temp in ToUpdate)
            {
                var result = model.mtblActiviteTypes.Where(a => a.code == temp.code).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.ActiviteType_Chi = temp.ActiviteType_Chi;
                    result.ActiviteType_Eng = temp.ActiviteType_Eng;
                    result.LastModifyBy = LastModifyBy;
                    result.LastModifyDate = DateTime.Now;
                }
            }
            foreach (var temp in ToDel)
            {
                var result = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID == temp).FirstOrDefault();
                {
                    if (result != null)
                    {
                        if (result.tblActivites.Count > 0)
                        {
                            foreach (var tempENT in result.tblActivites)
                            {
                                model.tblActivites.DeleteObject(tempENT);
                            }
                        }
                        model.mtblActiviteTypes.DeleteObject(result);
                    }
                }
            }
            return model.SaveChanges();
        }

        public List<mtblActiviteType> GetListByENT(mtblActiviteType ent)
        {
            var result = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.code))
                result = result.Where(a=>a.code==ent.code);
            return result.OrderByDescending(a=>a.ActiviteTypeUID).ToList();
        }
        public List<mtblActiviteType> GetHKEAListByENT(string strCode)
        {
            var result = model.mtblActiviteTypes.Where(a=>!string.IsNullOrEmpty(a.code));
            if (!string.IsNullOrEmpty(strCode))
            {
                result = result.Where(a=>a.code==strCode);
            }
            return result.ToList();
        }

        public int GetCountByENT(mtblActiviteType ent)
        {
            var result = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.ActiviteTypeUID > 0)
                result = result.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.code))
                result = result.Where(a => a.code == ent.code);
            return result.Count();

        }

        public int UpdateENT(mtblActiviteType ent)
        {
            activitetype = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID).FirstOrDefault();
            if (activitetype != null)
            {
                activitetype.ActiviteType_Chi = ent.ActiviteType_Chi;
                activitetype.ActiviteType_Eng = ent.ActiviteType_Eng;
                activitetype.Deleted = ent.Deleted;
                activitetype.LastModifyBy = ent.LastModifyBy;
                activitetype.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblActiviteType ent)
        {
            activitetype = model.mtblActiviteTypes.Where(a => a.ActiviteTypeUID == ent.ActiviteTypeUID).FirstOrDefault();
            if (activitetype != null)
            {
                model.mtblActiviteTypes.DeleteObject(activitetype);
            }
            return model.SaveChanges();
        }
    }
}
